package com.up.vms.application.service;

import com.up.vms.domain.module.VideoDTO;
import com.up.vms.domain.vm.entity.Video;
import com.up.vms.domain.vm.enums.CategoryStatus;
import com.up.vms.domain.vm.service.VideoService;
import com.up.vms.infrastructure.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import com.up.vms.application.common.DomainUtils;
import com.up.vms.domain.module.CategoryDTO;
import com.up.vms.domain.vm.entity.Category;
import com.up.vms.domain.vm.service.CategoryService;
import com.up.vms.infrastructure.mvc.GeneRespDTO;
import com.up.vms.infrastructure.mvc.RespCode;
import com.up.vms.infrastructure.mvc.ReturnCode;
import com.up.vms.infrastructure.util.CommonUtils;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class CategoryApplicationService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private VideoService videoService;

    public GeneRespDTO<HashMap> getCategoryList(CategoryDTO categoryDTO) throws Exception {
        GeneRespDTO geneRespDTO = null;

        Page<Category> simpleBanners = null;
        if (CommonUtils.isNotNull(categoryDTO.getCategory())) {
            simpleBanners = categoryService.findAllByBannerName(categoryDTO);
        } else if (CommonUtils.isNotNull(categoryDTO.getStatus())) {
            simpleBanners = categoryService.findAllByStatus(categoryDTO);
        } else {
            simpleBanners = categoryService.findAllByPageable(categoryDTO);
        }

        if (CommonUtils.isNotNull(simpleBanners)) {
            geneRespDTO = DomainUtils.convert(simpleBanners);
        } else {
            throw new Exception("没有记录");
        }
        return geneRespDTO;
    }

    public GeneRespDTO saveCategory(Category category) throws Exception {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        Category saveCategory = null;
        if (CommonUtils.isNotNull(category.getCategoryId())) {
            saveCategory = categoryService.findCategoryById(category.getCategoryId());
            saveCategory.setCategory(category.getCategory());
            saveCategory.setStatus(category.getStatus());
            saveCategory.setSorting(category.getSorting());
            saveCategory.setModified(new Date());
        } else {
            saveCategory = category;
        }

        Category newCategory = categoryService.save(saveCategory);

        geneRespDTO.setData(newCategory);
        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));

        return geneRespDTO;
    }

    public GeneRespDTO open(List<String> categoryIdList) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        for (String idStr : categoryIdList) {
            Long categoryId = Long.parseLong(idStr);
            Category category = categoryService.findCategoryById(categoryId);
            category.setStatus(CategoryStatus.AVAILABLE.code);
            categoryService.save(category);
        }
        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(categoryIdList);
        return geneRespDTO;
    }

    public GeneRespDTO close(List<String> categoryIdList) {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        for (String idStr : categoryIdList) {
            Long categoryId = Long.parseLong(idStr);
            Category category = categoryService.findCategoryById(categoryId);
            category.setStatus(CategoryStatus.DISABLE.code);
            categoryService.save(category);
        }
        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(categoryIdList);
        return geneRespDTO;
    }

    public GeneRespDTO delete(List<String> categoryIdList) throws BusinessException {
        GeneRespDTO geneRespDTO = new GeneRespDTO();
        for (String idStr : categoryIdList) {
            Long categoryId = Long.parseLong(idStr);
            Category category = categoryService.findCategoryById(categoryId);
            if (CommonUtils.isNotNull(category)) {
                VideoDTO videoDTO = new VideoDTO();
                videoDTO.setCategoryId(categoryId);
                long count = videoService.countByCategoryId(videoDTO);
                if (count > 0) {
                    throw new BusinessException(String.format("分类[%s]下有视频", category.getCategory()));
                } else {
                    categoryService.delete(category);
                }
            }
        }
        geneRespDTO.setRespCode(new RespCode(ReturnCode.SUCCESS));
        geneRespDTO.setData(categoryIdList);
        return geneRespDTO;
    }
}
